package com.example.tests;

import static com.example.tests.GroupDataGenerator.loadGroupsFromXmlFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class GroupCreationTests extends TestBase
{
	@DataProvider
	public Iterator<Object[]> groupsFromFile() throws IOException
	{
		return wrapGroupDataForDataProvider(loadGroupsFromXmlFile(new File("groups.xml"))).iterator();
	}

	@Test(dataProvider = "randomValidGroupGenerator")
	public void testGroupCreation(GroupData group) throws Exception
	{
		SortedListOf<GroupData> oldList = app.getModel().getGroups();

		app.getGroupHelper().createGroup(group);

		SortedListOf<GroupData> newList = app.getModel().getGroups();

		assertThat(newList, equalTo(oldList.withAdded(group)));

		if (timeToCheck())
		{
			if ("yes".equals(app.getProperty("check.db")))
			{
				assertThat(newList, equalTo(app.getHibernateHelper().getGroups()));
			}
			if ("yes".equals(app.getProperty("check.ui")))
			{
				assertThat(newList, equalTo(app.getGroupHelper().getUIGroups()));
			}
		}

	}
}
